//
//  NSDate+AFF.m
//  AnyfishApp
//
//  Created by Bob Lee on 15/9/6.
//  Copyright (c) 2015年 Anyfish. All rights reserved.
//

#import "NSDate+AFF.h"
#import "regex.h"
#import "NSDate+Formatter.h"

@implementation NSDate (AFF)
+ (NSDate*)dateAF {
    NSDate *date = [NSDate date];

    
    return date;
}

+ (U64)timeMicro {

    return [[NSDate date] timeIntervalSince1970]*1000;
}

+ (U32)timeSecond {

    return [[NSDate date] timeIntervalSince1970];
}

+ (U64)timeMicro:(NSDate*)date {
    return [date timeIntervalSince1970]*1000;
}

+ (U32)timeSecond:(NSDate*)date {
    return [date timeIntervalSince1970];
}

+ (NSDate *)string2Date:(NSString *)str format:(AFFTimeFormatType)type {
    NSDateFormatter *dateFormatter = [NSDate formatter];
    [dateFormatter setDateFormat:[self formatString:type]];
    NSDate *dateTime = [dateFormatter dateFromString:str];
    return dateTime;
}

+ (NSDate *)string2Date:(NSString *) str {
    return [self string2Date:str format:ETimeFormatTimeCommon];
}

+ (NSString *)currentTimeString:(AFFTimeFormatType) type {
    NSDateFormatter *dateFormatter = [NSDate formatter];
    [dateFormatter setDateFormat:[self formatString:type]];
    
    NSString *curTime = [dateFormatter stringFromDate:[NSDate date]];
    
    return curTime;
}

+ (NSString*)formatString:(AFFTimeFormatType)type {
    if(type == ETimeFormatTimeDate){
        return @"yyyy-MM-dd";
    }else if(type == ETimeFormatTimeDate_CN){
        return @"yyyy年MM月dd日";
    }
    else if(type == ETimeFormatTimeDateEx)
    {
        return @"yyyy.MM.dd";
    }
    else if(type == ETimeFormatTimeCommon){
        return @"yyyy-MM-dd HH:mm:ss";
    }
    else if(type == ETimeFormatTimeShort){
        return @"yyyy-MM-dd HH:mm";
    }else if(type == ETimeFormatTimeTime){
        return @"HH:mm";
    }else if(type == ETimeFormatTimeYear){
        return @"yyyy";
    }else if(type == ETimeFormatTimeMonth){
        return @"MM";
    }else if(type == ETimeFormatTimeDay){
        return @"dd";
    }else if(type == ETimeFormatTimeHour){
        return @"HH";
    }else if(type == ETimeFormatTimeMinute){
        return @"mm";
    }else if(type == ETimeFormatTimeSecond){
        return @"ss";
    }else if(type == ETimeFormatTimeLong){
        return @"yyyy-MM-dd HH:mm:ss.SSS";
    }else if(type == ETimeFormatTimeMask){
        return @"yyyyMMddHHmmssSSS";
    }else if(type == ETimeFormatTimeShortDate){
        return @"MM-dd";
    }else if(type == ETimeFormatTimeShortDateTime){
        return @"MM-dd HH:mm";
    }else if(type == ETimeFormatTimeShortDateTime_CN){
        return @"M月d日 HH:mm";
    }else if(type == ETimeFormatTimeShortDate_CN){
        return @"M月d日";
    }else if(type == ETimeFormatTimeDateDotSpan){
        return @"yyyy.MM.dd";
    }else if(type == ETimeFormatTimeMonth_CN){
        return @"yyyy年MM月";
    }
    
    return @"yyyy-MM-dd HH:mm";
}

+ (NSString *)date2StringWithInterval:(NSInteger)interval formatType:(AFFTimeFormatType)type {
    return [self date2String:[NSDate dateWithTimeIntervalSince1970:interval] formatType:type];
}

+ (NSString *)date2String:(NSDate *)date formatType:(AFFTimeFormatType)type {
    if(date==nil)
        return nil;
    
    NSDateFormatter *dateFormatter = [NSDate formatter];
    [dateFormatter setDateFormat:[self formatString:type]];
    
    NSString *curTime = [dateFormatter stringFromDate:date];
    
    return curTime;
}

+ (NSString*)date2String:(NSDate *)date {
    return [self date2String:date isMessage:NO];
}

+ (NSString *)date2String:(NSDate*)date isMessage:(BOOL)isMessage {
    NSString *formatStr = nil;
    
    //1.获取当前时间所在的年月日，时分
    NSString *dateStr = [self currentTimeString:ETimeFormatTimeShort];
    if([NSString isNilOrEmpty:dateStr])return  @"";
    
    NSArray *dateArr = [[dateStr componentsSeparatedByString:@" "][0] componentsSeparatedByString:@"-"];
    NSArray *timeArr = [[dateStr componentsSeparatedByString:@" "][1] componentsSeparatedByString:@":"];
    if(dateArr.count==0 || timeArr.count==0)
        return @"";
    
    NSInteger yearNow = [[dateArr[0] substringFromIndex:2] integerValue],monthNow = [dateArr[1] integerValue],dayNow = [dateArr[2] integerValue];
    //    NSInteger hourNow = [timeArr[0] integerValue],minuteNow = [timeArr[1] integerValue];
    
    
    //2.获取目标时间所在年月日，时分
    dateStr = [self date2String:date formatType:ETimeFormatTimeShort];
    
    if([NSString isNilOrEmpty:dateStr]){
        return @"时间格式错误";
    }
    
    dateArr = [[dateStr componentsSeparatedByString:@" "][0] componentsSeparatedByString:@"-"];
    timeArr = [[dateStr componentsSeparatedByString:@" "][1] componentsSeparatedByString:@":"];
    if(dateArr.count==0 || timeArr.count==0)
        return @"";
    
    NSInteger yearTarget = [[dateArr[0] substringFromIndex:2] integerValue],monthTarget = [dateArr[1] integerValue],dayTarget = [dateArr[2] integerValue];
    
    NSInteger hourTarget = [timeArr[0] integerValue],minuteTarget = [timeArr[1] integerValue];
    
    //3.判断是否本年
    if(yearNow>yearTarget){
        if((abs(12-monthTarget+monthNow)>3) || (yearNow-1)>yearTarget){
            formatStr = [NSString stringWithFormat:@"%02d-%02d-%02d",yearTarget,monthTarget,dayTarget];
        }
    }else{
        if(monthNow==monthTarget){
            if(dayNow-2 == dayTarget){
                // 前天
                formatStr = [NSString stringWithFormat:@"%@ %02d:%02d",kAFLocalize(kStrBefYesterday) ,hourTarget,minuteTarget];
            }else if(dayNow-1 == dayTarget){
                // 昨天
                formatStr = [NSString stringWithFormat:@"%@ %02ld:%02d",kAFLocalize(kStrYesterday),(long)hourTarget,minuteTarget];
            }else if(dayNow == dayTarget){
                // 当天
                formatStr = [NSString stringWithFormat:@"%02d:%02d",hourTarget,minuteTarget];
            }
        }
        
        
    }
    
    if(formatStr==nil){
//        if(isMessage)
//            formatStr = [NSString stringWithFormat:@"%02d-%02d", monthTarget,dayTarget];
//        else
            formatStr = [NSString stringWithFormat:@"%02d-%02d %02d:%02d", monthTarget,dayTarget,hourTarget,minuteTarget];
    }
    
    return formatStr;
}

+ (NSString *)date2StringWithInterval:(NSInteger)interval isMessage:(BOOL)isMessage {
    return [self date2String:[NSDate dateWithTimeIntervalSince1970:interval] isMessage:isMessage];
}

+ (NSString*)date2StringWithInterval:(NSInteger)interval {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    return [self date2String:date];
}

+ (AFFWeekDayType)getWeekDay:(NSDate *)date desc:(NSString **)desc {
    NSInteger week = [date weekday];
    
    NSString*weekStr=nil;
    AFFWeekDayType weekDayType = EWeekDayTypeMonday;
    if(week==1)
    {
        weekStr = kStrSunday;
        weekDayType = EWeekDayTypeSunDay;
    }else if(week==2){
        weekStr = kStrMonday;
        weekDayType = EWeekDayTypeMonday;
    }else if(week==3){
        weekStr = kStrThursday;
        weekDayType = EWeekDayTypeTuesday;
    }else if(week==4){
        weekStr = kStrWednesday;
        weekDayType = EWeekDayTypeWednesday;
    }else if(week==5){
        weekStr = kStrThursday;
        weekDayType = EWeekDayTypeThursday;
    }else if(week==6){
        weekStr = kStrFriday;
        weekDayType = EWeekDayTypeFriday;
    }else if(week==7){
        weekStr = kStrSaturday;
        weekDayType = EWeekDayTypeSaturday;
    }
    
    if(desc)
        *desc = kAFLocalize(weekStr);
    
    return weekDayType;
}

+ (AFFWeekDayType)getCurrentWeekDay:(NSString **) desc {
    //得到当前的日期--得到星期几
    NSDate *date = [NSDate date];
    
    AFFWeekDayType weekType = [self getWeekDay:date desc:desc];
    return weekType;
}

+ (NSDate*)dateLocal {
    NSDate *date = [NSDate date];
   return [NSDate dateLocalEx:date];
}

+ (NSDate*)dateLocal:(NSTimeInterval) secs{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:secs];
    return [NSDate dateLocalEx:date];
}

+ (NSDate*)dateLocalEx:(NSDate*)date {
    return date;
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    return [date  dateByAddingTimeInterval:interval];
}

- (BOOL)isToday {
    NSDate *date = [NSDate dateLocal];
    NSString *dateNow = [NSDate date2String:date formatType:ETimeFormatTimeDate];
    NSString *dateSelf = [NSDate date2String:self formatType:ETimeFormatTimeDate];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit |
    NSMonthCalendarUnit |
    NSDayCalendarUnit |
    NSHourCalendarUnit |
    NSMinuteCalendarUnit |
    NSSecondCalendarUnit;
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:[NSDate date]];
    NSDate *temp = [NSDate dateWithTimeIntervalSince1970:(interval)];
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:temp];
    NSInteger fecordYear   = [dateComponent year];
    NSInteger fecordMonth  = [dateComponent month];
    NSInteger fecordDay    = [dateComponent day];
    NSInteger fecordHour   = [dateComponent hour];
    NSInteger fecordMinute = [dateComponent minute];
    NSInteger fecordSecond = [dateComponent second];
    
    return [dateNow isEqualToString:dateSelf];
}

@end
